package com.sum.slike.simple;

import ohos.agp.components.*;
import ohos.app.Context;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/26
 */
public class CommentAdapter extends BaseItemProvider {
    private boolean[] likeStatusArray = new boolean[10];
    private int[] likeNumArray = new int[10];
    private AdapterItemListener<Boolean> adapterItemListener;
    private LayoutScatter mLayoutScatter;

    public CommentAdapter(Context context) {
        this.mLayoutScatter = LayoutScatter.getInstance(context);
    }

    /**
     * setAdapterItemListener
     * @param adapterItemListener adapterItemListener
     */
    public void setAdapterItemListener(AdapterItemListener<Boolean> adapterItemListener) {
        this.adapterItemListener = adapterItemListener;
    }

    @Override
    public int getCount() {
        return likeStatusArray.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        CommentViewHolder holder;
        if (component == null) {
            holder = new CommentViewHolder();
            component = mLayoutScatter.parse(ResourceTable.Layout_item_comment_layout, componentContainer, false);
            holder.userName = (Text) component.findComponentById(ResourceTable.Id_user_name);
            holder.sendTime = (Text) component.findComponentById(ResourceTable.Id_send_time);
            holder.content = (Text) component.findComponentById(ResourceTable.Id_content);
            holder.likeCount = (Text) component.findComponentById(ResourceTable.Id_like_count);
            holder.like_status_layout = (DependentLayout) component.findComponentById(ResourceTable.Id_like_status_layout);
            holder.likeStatus = component.findComponentById(ResourceTable.Id_like_status);
            component.setTag(holder);
        } else {
            holder = (CommentViewHolder) component.getTag();
        }
        holder.userName.setText("管理员");
        holder.sendTime.setText("刚刚");
        holder.content.setText("哎哟不错哟");
        holder.likeCount.setText(likeNumArray[position] + "");
        holder.likeStatus.setSelected(likeStatusArray[position]);
        holder.like_status_layout.setClickedListener(c -> {
            if (adapterItemListener != null) {
                adapterItemListener.onItemClickListener(likeStatusArray[position], position, c.getId(), c);
            }
        });
        return component;
    }

    /**
     * updateLikeStatusByPosition
     * @param isLike isLike
     * @param position position
     */
    public void updateLikeStatusByPosition(final boolean isLike, final int position) {
        likeStatusArray[position] = isLike;
        notifyDataSetItemChanged(position);
    }

    /**
     * updateLikeNumByPosition
     * @param isLike isLike
     * @param position position
     */
    public void updateLikeNumByPosition(boolean isLike, int position) {
        int itemPos = likeNumArray[position];
        if (isLike) {
            itemPos++;
        } else {
            itemPos--;
        }
        likeNumArray[position] = itemPos;
        notifyDataSetItemChanged(position);
    }

    /**
     *
     * @param <T>
     */
    public interface AdapterItemListener<T> {
        void onItemClickListener(T data, int position, int id, Component component);
    }

    /**
     *
     */
    static class CommentViewHolder {
        Text userName;
        Text sendTime;
        Text content;
        Text likeCount;
        DependentLayout like_status_layout;
        Component likeStatus;
    }
}
